package com.panghl.report;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

/**
 * @Author panghl
 * @Date 2022/3/3
 * @Description 把word转成pdf
 *
 * Exception in thread "main" java.lang.UnsatisfiedLinkError: no jacob-1.19-x64 in java.library.path
 *
 * 将jacob-1.1864.dll文件复制到当前使用JDK的bin目录下即可
 **/
public class JacobDemo {
    public static void main(String[] args) {
        String source = "D:\\张三_合同.docx";
        String target = "D:\\张三_合同.pdf";
        ActiveXComponent app = null;
        try {
            //调用window中的office程序
            app = new ActiveXComponent("Word.application");
            //调用word时不显示窗口
            app.setProperty("Visible", false);
            // 获取文档
            Dispatch docs = app.getProperty("Documents").toDispatch();
            //打开指定的文档
            // 获取到某个文件
            Dispatch doc = Dispatch.call(docs, "Open", source).toDispatch();
            //调用文件的另存为功能 宏值 17
            Dispatch.call(doc, "SaveAs", target, 17);
            //调用关闭  关闭word
            Dispatch.call(doc, "Close");
        } catch (Exception e) {
            System.out.println("Word转PDF出错：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 关闭office
            if (app != null) {
                app.invoke("Quit", 0);
            }
        }

    }
}
